
import conf from '../config/userconf.js';
import { quillEditor } from 'vue-quill-editor'

export default{
    
    data(){
        var validatePhone = (rule,value,callback) => {    //正则手机号

            var MobileRegex = /^1[3|4|5|7|8][0-9]{9}$/;
            if (!MobileRegex.test(value)) {
                callback(new Error('手机号格式不正确！'))
            } else {
                callback();
            }
        };
        var validateTel = (rule,value,callback) => {    //正则座机号
            var MobileRegex = /(^(\d{3,4}-)\d{7,8}$)|(^\d{7,8}$)/;
            if (!MobileRegex.test(value)) {
                callback(new Error('电话格式不正确！'))
            } else {
                callback();
            }
        };

        return{
            list:[],  //列表数据
            searchData:{ //查询
                a_type:'',
                a_title:'',
            },
            form: {    //文章新增、修改  表单数据
                a_type:'',
                a_title:'',
                a_content:'',
                a_phone: '',
            },
            dialogtitle: '添加文章',  //弹框标题
            dialogFormVisible: false,  //嵌套表格隐藏展示
            rules:{   //表单验证
                a_type: [
                    { required: true, message: '请选择类型', trigger: 'change' }
                ],
                a_title: [
                    { required: true, message: '请输入标题', trigger: 'blur' }
                ],
                a_content: [
                    { required: true, message: '请输入内容', trigger: 'blur' }
                ],
                a_phone: [
                    { required: false, message: '请输入联系人手机号', trigger: 'blur' },
                    {
                        validator:(rule,value,callback)=>{
                            if(value != ""){
                                if(value.length < 11 ){
                                    validateTel(rule,value,callback)
                                }else if(value.length >= 11){
                                    validatePhone(rule,value,callback)
                                }
                                
                            }else{
                                callback();
                            }
                        }, trigger: 'blur'
                    },
                ],
            },
            edit_id: '',  //编辑    账号id
            editorOption: {
                placeholder: "请编辑内容",
                modules: {
                    toolbar: [
                        ['bold', 'italic', 'underline', 'strike'],
                        ['blockquote', 'code-block'],
                        [{ 'header': 1 }, { 'header': 2 }],
                        [{ 'list': 'ordered' }, { 'list': 'bullet' }],
                        [{ 'script': 'sub' }, { 'script': 'super' }],
                        [{ 'indent': '-1' }, { 'indent': '+1' }],
                        [{ 'direction': 'rtl' }],
                        [{ 'size': ['small', false, 'large', 'huge'] }],
                        [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
                        [{ 'color': [] }, { 'background': [] }],
                        [{ 'font': [] }],
                        [{ 'align': [] }],
                        ['clean'],
                        ['link', 'image', 'video']
                    ]
                }
            },
        }
    },
    methods:{
        onEditorBlur(){//失去焦点事件
        },
        onEditorFocus(){//获得焦点事件
        },
        onEditorChange(){//内容改变事件
        },
        /* 列表*/
        getList (data=Object()){
            conf.articlelist.list(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    this.list = res.data.data
                }else{
                    this.list = [];
                    this.$message({
                        type: 'info',
                        message: res.data.msg
                    });
                }
            })
        },
        //增
        add(){
            // 设置弹框标题
            this.dialogtitle = '添加文章';
            this.dialogFormVisible = true;  
        },
        //修改账号信息（编辑）
        edit(val) {
            this.form = {
                a_type: val.a_type.toString(),
                a_title: val.a_title,
                a_content: val.a_content,
                a_phone: val.a_phone,
            },
            this.edit_id = val.a_id;
            // 设置弹框标题
            this.dialogtitle = '编辑文章';
            this.dialogFormVisible = true;  
        },
        // 查
        search () {
            let data  = this.searchData;

            this.getList(data)
            // this.$router.push({name:'pay'})
        },
        //启用禁用
        updatestatus(row,data=Object()){
            // alert(row.a_status)
            this.$confirm('确认更改文章 "'+row.a_title+'的状态吗" ?', '更改状态', {  
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                data.a_id = row.a_id;    //文章id
                data.a_status = row.a_status;
                conf.articlelist.updatestatus(data,(state,res)=>{
                    if(state == 'success'&&res.data.code == 0){
                        // console.log(res);
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '更改成功!'
                        });
                    }else if(state == 'success'&&res.data.code != 0){
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消更改'
                });          
            });
        },
        //删除
        del(row,data=Object()){

            this.$confirm('确认删除文章 "'+row.a_title+'" ?', '删除文章', {  
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                data.a_id = row.a_id;    //文章id
                conf.articlelist.del(data,(state,res)=>{
                    if(state == 'success'&&res.data.code == 0){
                        // console.log(res);
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }else if(state == 'success'&&res.data.code != 0){
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消删除'
                });          
            });
        },
        // 状态
        statusF (row, column, cellValue, index) {
            switch (cellValue) {
                case 1:
                    return '启用'
                case 2:
                    return '禁用'
            }
        },
        // 类型
        typeF (row, column, cellValue, index) {
            switch (cellValue) {
                case 1:
                    return '公司简介'
                case 2:
                    return '新闻中心 '
                case 3:
                    return '使用帮助 '
            }
        },
        //表单提交
        submitForm(formName) {
            if(this.form.a_type != 1){  //类型非公司简介时，电话为空
                this.form.a_phone = '';
            }
            this.$refs[formName].validate((valid) => {
              if (valid) {

                if (this.dialogtitle == '添加文章') {
                    
                    //请求
                    conf.articlelist.add(this.form,(state,res)=>{
                        if(state == 'success'&&res.data.code == 0){
                            this.getList();
                            this.$message({
                                type: 'success',
                                message: '添加成功'
                            });
                            this.dialogFormVisible = false
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.data.msg
                            });
                        }
                    })
                }else{  //编辑账号信息

                    //请求
                    const data = this.form;
                    data.a_id = this.edit_id;
                    conf.articlelist.edit(data,(state,res)=>{
                        if(state == 'success'&&res.data.code == 0){
                            this.getList();
                            this.$message({
                                type: 'success',
                                message: '编辑成功'
                            });
                            this.dialogFormVisible = false
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.data.msg
                            });
                        }
                    })
                }
                    
              } else {
                // console.log('error submit!!');
                return false;
              }
            });
        },
        // 取消提交
        cancel(formName){
            this.dialogFormVisible = false;
            if (formName) {
                this.$refs[formName].resetFields();
            }
        },
    },
    mounted (){
        this.$nextTick(function(){
            this.getList()
        })
    },
}